package cn.it.ygh.annotation;

import java.lang.reflect.Method;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.it.ygh.pojo.Admin;
import cn.it.ygh.pojo.Log;
import cn.it.ygh.service.LogService;

@Aspect
@Component
public class LogAspect {
	@Resource
	private LogService logService;
    @Autowired
    private HttpSession session;

    @Autowired
    private HttpServletRequest request;
	
	@Pointcut("@annotation(cn.it.ygh.annotation.UserLog)")
	public void UserAspect(){
		
	}
	@Pointcut("@annotation(cn.it.ygh.annotation.SystemLog)")
	public void SystemAspect(){
		
	}
	
	@Before("UserAspect()")
	public void doBefore(JoinPoint joinPoint){
	
		Admin admin = (Admin)session.getAttribute("admin");
		String ip = request.getRemoteAddr();
		try{
			System.out.println("后置通知开始");
			System.out.println("请求方法:"+(joinPoint.getTarget().getClass().getName()+"."+joinPoint.getSignature().getName()+"()"));
			System.out.println("方法描述:"+getUserMethodDescription(joinPoint));
			System.out.println("请求人:"+admin.getAdminName());
			System.out.println("请求IP:"+ip);
			
			Log log = new Log();
			log.setLoginIp(ip);
			log.setOperatorAccount(admin.getAdminName());
			log.setOperatorClass("管理员");
			log.setOperateDesc(getUserMethodDescription(joinPoint));
			log.setOperateTime(new Date());
			
			logService.saveLog(log);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	@After("SystemAspect()")
	public void doAfter(JoinPoint joinPoint){
		
		Admin admin = (Admin)session.getAttribute("admin");
		String ip = request.getRemoteAddr();
		try{
			System.out.println("后置通知开始");
			System.out.println("请求方法:"+(joinPoint.getTarget().getClass().getName()+"."+joinPoint.getSignature().getName()+"()"));
			System.out.println("方法描述:"+getSystemMethodDescription(joinPoint));
			System.out.println("请求人:"+admin.getAdminName());
			System.out.println("请求IP:"+ip);
			
			Log log = new Log();
			log.setLoginIp(ip);
			log.setOperatorAccount(admin.getAdminName());
			log.setOperatorClass("管理员");
			log.setOperateDesc(getSystemMethodDescription(joinPoint));
			log.setOperateTime(new Date());
			
			logService.saveLog(log);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	 public  static String getUserMethodDescription(JoinPoint joinPoint)  throws Exception {    
        String targetName = joinPoint.getTarget().getClass().getName();    
        String methodName = joinPoint.getSignature().getName();    
        Object[] arguments = joinPoint.getArgs();    
        Class targetClass = Class.forName(targetName);    
        Method[] methods = targetClass.getMethods();    
        String description = "";    
         for (Method method : methods) {    
             if (method.getName().equals(methodName)) {    
                Class[] clazzs = method.getParameterTypes();    
                 if (clazzs.length == arguments.length) {    
                    description = method.getAnnotation(UserLog. class).description();    
                     break;    
                }    
            }    
        }    
	    return description;    
	}    
	 
	 public  static String getSystemMethodDescription(JoinPoint joinPoint)  throws Exception {    
        String targetName = joinPoint.getTarget().getClass().getName();    
        String methodName = joinPoint.getSignature().getName();    
        Object[] arguments = joinPoint.getArgs();    
        Class targetClass = Class.forName(targetName);    
        Method[] methods = targetClass.getMethods();    
        String description = "";    
         for (Method method : methods) {    
             if (method.getName().equals(methodName)) {    
                Class[] clazzs = method.getParameterTypes();    
                 if (clazzs.length == arguments.length) {    
                    description = method.getAnnotation(SystemLog.class).description();    
                     break;    
                }    
            }    
        }    
        return description;    
    }    
	
}
